package Sys_Module;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.locks.LockSupport;

public class Server_Network implements Runnable {
    private int port;
    private ServerSocket server;
    private Socket socket_server;
    private boolean force_stop = false;
    private List_Main LM;
    private Log_Module Log_Sys;

    public Server_Network(String ip, int port1, List_Main LM, Log_Module Log_Sys) {
        this.port = port1;
        this.LM = LM;
        this.Log_Sys = Log_Sys;
        // System.out.println("RUN Thread" + port1);
        try {
            server = new ServerSocket(port1);
            // server.setSoTimeout(20000);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                Log_Sys.Write_log("Server Network info", "SetPort Connect Complete Port:" + server.getLocalPort());
            } catch (IOException e) {
            }
        }
    }

    public synchronized void output(String text) {// 发送数据
        try {
            OutputStream outdata = socket_server.getOutputStream();
            DataOutputStream outdatastream = new DataOutputStream(outdata);
            outdatastream.writeUTF(text);
            Log_Sys.Write_log("Server Network Send info(" + server.getLocalPort() + ")",
                    "Dst.IP:" + socket_server.getRemoteSocketAddress() + " Dst.port:" + socket_server.getPort()
                            + " Length:" + text.length()); // 输出日志

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String format_data(String text, int lx) { // 类型(info/request/response)#窗口(1-5)$信息
        if (lx == 0) {
            if (text.indexOf("#") != -1 && (text.indexOf("$") - text.indexOf("#") != 1)) {
                return "OK";
            }
        }
        if (lx == 1) {
            return text.substring(0, text.indexOf("#"));
        }
        if (lx == 2) {
            return text.substring(text.indexOf("#") + 1, text.indexOf("$"));
        }
        if (lx == 3) {
            return text.substring(text.indexOf("$") + 1, text.length());
        }
        return "";
    }

    public void run() {
        try {
            socket_server = server.accept(); // 接收各个客户端的请求
            InputStream indata = socket_server.getInputStream();//
            DataInputStream indatastream = new DataInputStream(indata);

            LM.Change_Client_Connect_Status(port); // 通过传递port端口号 更改list_Main中的客户端状态

            while (true) {
                String temp = indatastream.readUTF();// 读取接收到的数据
                if (temp.length() != 0) {// 判断接收到的数据长度 如果不小于0 进入里面
                    Log_Sys.Write_log("Server Network Receive info(" + server.getLocalPort() + ")",
                            "Dst.IP:" + socket_server.getRemoteSocketAddress() + " Dst.port:" + socket_server.getPort()
                                    + " Length:" + temp.length()); // 输出日志

                    if (temp.equals("putin") && port == 48002) {// 判断端口号
                        // th.notify();
                        output("你的序号是:" + LM.num);// 输出端口号
                        LockSupport.unpark(LM.Wait_list_thread);// 恢复线程
                    }

                    if (port == 48001) {
                        if (format_data(temp, 0).equals("OK")) {// 判断数据有效性
                            if (format_data(temp, 1).equals("response")) {// 如果收到response请求
                                if (format_data(temp, 3).equals("comein")) {// 如果收到取号机取号请求
                                    LM.worker_windows.Change_Windows_Status(Integer.valueOf(format_data(temp, 2)), 2);// 更新状态
                                }
                                if (format_data(temp, 3).equals("fail")) {// 如果收到取号机过号请求
                                    LM.worker_windows.Change_Windows_Status(Integer.valueOf(format_data(temp, 2)), 0);// 更新状态
                                }
                            }
                        }
                    }

                    if (port == 48003) {
                        if (format_data(temp, 0).equals("OK")) {// 检测数据的有效性
                            if (format_data(temp, 1).equals("response")) {
                                if (format_data(temp, 3).equals("done")) {// 如果收到工作窗口完成的信息
                                    LM.worker_windows.Change_Windows_Status(Integer.valueOf(format_data(temp, 2)), 0);// 更新状态
                                }
                            }

                        }
                    }
                    if (force_stop == true)// 强行停止
                        break;
                    // System.out.println(temp);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
